package com.leoman.resource.service.impl;

import com.leoman.adsindex.dao.AdsIndexDao;
import com.leoman.adsindex.entity.AdsIndex;
import com.leoman.banner.dao.BannerDao;
import com.leoman.banner.entity.Banner;
import com.leoman.common.service.impl.GenericManagerImpl;
import com.leoman.entity.Constant;
import com.leoman.floatwin.dao.FloatWinDao;
import com.leoman.floatwin.entity.FloatWin;
import com.leoman.resource.service.GameService;
import com.leoman.resources.dao.GameResourcesDao;
import com.leoman.resources.entity.GameResource;
import com.leoman.resources.entity.MusicResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by 蔡琦 on 2016/6/28 0028.
 *
 */
@Service
public class GameServiceImpl extends GenericManagerImpl<GameResource, GameResourcesDao> implements GameService {

    @Autowired
    private GameResourcesDao gameResourcesDao;

    @Autowired
    private BannerDao bannerDao;

    @Autowired
    private AdsIndexDao adsIndexDao;

    @Autowired
    private FloatWinDao floatWinDao;

    @Override
    public Page<GameResource> page(Integer pageNum, Integer pageSize) {

        return gameResourcesDao.findAll(new PageRequest(pageNum - 1,pageSize, Sort.Direction.DESC,"id"));
    }

    @Override
    public Integer deleteGame(Long gameId) {

        // 是否链接到广告banner
        List<Banner> bannerList = bannerDao.findMusicAndGame(Constant.POSITION_004, Constant.RESOURCE_TYPE_003, gameId);
        if (bannerList.size() != 0) {
            return 2;
        }

        // 是否链接到首页推荐
        List<AdsIndex> adsIndexList = adsIndexDao.findMusicAndGame(Constant.POSITION_004, Constant.RESOURCE_TYPE_003, gameId);
        if (adsIndexList.size() != 0) {
            return 2;
        }

        // 是否链接到浮窗广告
        List<FloatWin> floatWinList = floatWinDao.findMusicAndGame(Constant.POSITION_004, Constant.RESOURCE_TYPE_003, gameId);
        if (floatWinList.size() != 0) {
            return 2;
        }

        GameResource gameResource = gameResourcesDao.findOne(gameId);
        delete(gameResource);
        return 1;
    }
}
